package com.unionx.unionxuf.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Repository;
import com.unionx.unionxuf.common.DynamicConnect;
import com.unionx.unionxuf.entity.Code;
import com.unionx.unionxuf.entity.Settlestyle;

@Repository("SubjectDao")
public class SubjectDaoImpl implements SubjectDao {
	private Statement stmt = null;
	private Connection conn = null;
	private Map<String, Object> map = new HashMap<String, Object>();


	public Object getSettle(HttpServletRequest request,Integer pageSize,Integer pageNum) {
		map.clear();
		try {
			conn = (Connection) DynamicConnect.getConnectionInfo(request);
			stmt = conn.createStatement();
			String sql = "select cSSCode,cSSName,bSSEnd from SettleStyle";
			List<Object> list = new ArrayList< Object>();
			
			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				Settlestyle s=new Settlestyle();
				s.setSettlementcode( rs.getString(1));
				s.setSettlement(rs.getString(2));
				s.setSettlementEnd( rs.getString(3));
				list.add(s);
			}
			  int re =0;
			  int total = 0;
			  List<Object> listre = new ArrayList< Object>();
			  for(int i=(pageNum-1)*pageSize;i<list.size();i++){
				  if(re==pageSize){
					  break;
				  }else{
				  listre.add(list.get(i)); 
				  	re++;
				  }
				 }
				
			  
			  int  count  =list.size();
				 if(pageSize>count){
				 total =1;
				 }
				 if(count%pageSize==0){
				 total=count/pageSize;
				 }
				 if(count%pageSize>0){
				 total=count/pageSize+1;
				 }

			map.put("status", 0);
			map.put("message", "方法请求成功");
			map.put("count", total);
			map.put("data", listre);
			return map;
		} catch (SQLException e) {

			map.put("status", 1);
			map.put("message", "方法请求失败");
			map.put("data", "500");
			return map;

		}
	}


	public Object getSubject(HttpServletRequest request) {
		map.clear();
		try {
			conn = (Connection) DynamicConnect.getConnectionInfo(request);
			stmt = conn.createStatement();
			String sql = "select ccode ,ccode_name,bend from code ";
			List<Code> list = new ArrayList<Code>();
			
		
			 int i =1; 

			ResultSet rs = stmt.executeQuery(sql);
		
				while(rs.next()){	
				Code code =  new Code();
				code.setId(i++);
				code.setCode(rs.getString(1));
				code.setCodename(rs.getString(2));
				code.setBend(rs.getString(3));
				
				list.add(code);
			}
			map.put("status", 0);
			map.put("message", "方法请求成功");
			map.put("Clist", list);
			return map;
		} catch (SQLException e) {

			map.put("status", 1);
			map.put("message", "方法请求失败");
			map.put("data", "500");
			return map;
		}
	}


	public Code getSubjectInfo(HttpServletRequest request,String code) {
		// TODO Auto-generated method stub
		Code c = new Code();
		try {
			conn = (Connection) DynamicConnect.getConnectionInfo(request);
			stmt = conn.createStatement();
			String sql = "select i_id,ccode ,ccode_name,bend from code where ccode="+code+"";
			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				
					c.setId(rs.getInt(1));
					c.setCode(rs.getString(2));
					c.setCodename(rs.getString(3));
					c.setBend(rs.getString(4));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return c;
	}

	
	public Object getSettleInfo(HttpServletRequest request) {
		// TODO Auto-generated method stub
		map.clear();
		List<Object> list = new ArrayList< Object>();
		try {
			conn = (Connection) DynamicConnect.getConnectionInfo(request);
			stmt = conn.createStatement();
			String sql = "select cSSCode,cSSName from SettleStyle where bSSEnd=1";
			
			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				Settlestyle s=new Settlestyle();
				s.setSettlementcode(rs.getString(1));
				s.setSettlement(rs.getString(2));
				list.add(s);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		map.put("data", list);
		return map;
	}


	public Object getSettlename(HttpServletRequest request,String scode) {
		map.clear();
		Settlestyle s =  new Settlestyle();
		try {
			conn = (Connection) DynamicConnect.getConnectionInfo(request);
			stmt = conn.createStatement();
			String sql = "select cSSName from SettleStyle where bSSEnd=1 and cSSCode="+scode+"";	
			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				s.setSettlement(rs.getString(1));
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		map.put("data", s);
		return map;
	}


	public Object getsubinfo(HttpServletRequest request) {	
		//select ccode  from code  where bend=1
		map.clear();
		List<Object> list = new ArrayList< Object>();
		try {
			conn = (Connection) DynamicConnect.getConnectionInfo(request);
			stmt = conn.createStatement();
			String sql = "select ccode  from code  where bend=1";
			
			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				Code c=new Code();
				c.setCode(rs.getString(1));
				list.add(c);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		map.put("data", list);
		return map;
	}

	
	public Object getsubname(HttpServletRequest request,String subcode) {
		// TODO Auto-generated method stub
		map.clear();
		Code c=new Code();
		try {
			conn = (Connection) DynamicConnect.getConnectionInfo(request);
			stmt = conn.createStatement();
			String sql = "select ccode_name  from code  where bend=1 and ccode="+subcode+" ";
			
			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				
				c.setCodename(rs.getString(1));
				
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		map.put("data", c);
		return map;
	}


	public Object getSettles(HttpServletRequest request) {
		
		// TODO Auto-generated method stub
				map.clear();
				List<Object> list = new ArrayList< Object>();
				try {
					conn = (Connection) DynamicConnect.getConnectionInfo(request);
					stmt = conn.createStatement();
					String sql = "select cSSName from SettleStyle where bSSEnd=1";
					
					ResultSet rs = stmt.executeQuery(sql);
					while (rs.next()) {
						Settlestyle s=new Settlestyle();
						s.setSettlement(rs.getString(1));
						list.add(s);
					}
				} catch (Exception e) {
					// TODO: handle exception
				}
				map.put("data", list);
				return map;
	}
}
